$(document).ready(function () {
    validatelogin();

    $('.login-form input[type="text"], .login-form input[type="password"], .login-form textarea')
        .on('focus', function () {
        $(this).removeClass('input-error');
    });

    $('.login-form').on('submit', function (e) {
        var flag = true;
        $(this).find('input[type="text"], input[type="password"], textarea').each(function () {
            if ($(this).val() === "") {
                e.preventDefault();
                $(this).addClass('input-error');
                flag = false;
            } else {
                $(this).removeClass('input-error');
            }
        });
        var username = $(this).find('input[type="text"]').val();
        var password = $(this).find('input[type="password"]').val();

        if (flag) {
            $.ajax({
                url: 'servlet/LoginServlet',
                type: 'post',
                dataType: 'json',
                data: {
                    username: username,
                    password: password
                },
                success: function (json) {
                    var result = json.result;
                    if (result) {//登录成功
                        Cookies.set('id', json.id, {expires: 1 / 24});
                        Cookies.set('operator', json.operator, {expires: 1 / 24});
                        window.location.href = "index.html";
                    } else {
                        window.wxc.xcConfirm("用户名或口令错误", window.wxc.xcConfirm.typeEnum.error);
                    }
                },
                error: function () {
                    window.wxc.xcConfirm("登录失败", window.wxc.xcConfirm.typeEnum.error);
                }
            });
        }
        return false;
    });

    $('.registration-form input[type="text"], .registration-form textarea').on('focus', function () {
        $(this).removeClass('input-error');
    });

    $('.registration-form').on('submit', function (e) {
        var flag = true;
        $(this).find('input[type="text"],input[type="password"]').each(function () {
            if ($(this).val() === "") {
                e.preventDefault();
                $(this).addClass('input-error');
                flag = false;
            } else {
                $(this).removeClass('input-error');
            }
        });
        if (flag) {
            var password1 = $('#form-password1').val();
            var password2 = $('#form-password2').val();
            if (password1 !== password2) {
                window.wxc.xcConfirm("两次口令不相同", window.wxc.xcConfirm.typeEnum.error);
                return false;
            }
            var username = $('#form-user-name').val();
            var nickname = $('#form-nickname').val();

            $.ajax({
                url: 'servlet/RegisterServlet',
                type: 'post',
                data: {
                    username: username,
                    password: password1,
                    nickname: nickname
                },
                success: function (json) {
                    if (json === '0') {
                        window.wxc.xcConfirm("注册新用户失败", window.wxc.xcConfirm.typeEnum.error);
                    } else if (json === '-1') {
                        window.wxc.xcConfirm("用户名已存在", window.wxc.xcConfirm.typeEnum.error);
                    } else {
                        window.wxc.xcConfirm("注册成功", window.wxc.xcConfirm.typeEnum.error);
                        Cookies.set("id", json);
                        Cookies.set("operator", nickname);
                        window.location.href = "index.html";
                    }
                },
                error: function () {
                    window.wxc.xcConfirm("注册发生错误", window.wxc.xcConfirm.typeEnum.error);
                }
            });

            return false;
        }
    });
});

function validatelogin() {
    var id = Cookies.get('id');
    if (undefined !== id) {
        window.location.href = "index.html";
    }
}
